/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) BlockCipherApi.java 2018-08-06 17:26
 */

package cn.jh.common.core.crypto;

/**
 * @author Fuchun
 * @since 1.0
 */
public abstract class BlockCipherApi extends BaseSymmetricCipherApi {

    protected BlockCipherApi(SymmetricCipherParams params) {
        super(params);
    }

    /**
     * Returns {@code true} if the specified cipher operation mode name supports initialization vectors,
     * {@code false} otherwise.
     *
     * @param mode the mode of operation
     * @return {@code true} if the specified cipher operation mode name supports initialization vectors,
     *         {@code false} otherwise.
     */
    private boolean isModeIvCompatible(OprMode mode) {
        return mode != null &&
                mode != OprMode.ECB && mode != OprMode.NONE;
    }

    @Override
    protected boolean isGenerateIV(boolean streaming) {
        return streaming || (getParams().isGenerateIv() && isModeIvCompatible(getParams().getMode()));
    }
}
